home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-12-08 | 2.9 KB | 102 lines | [TEXT/GEOL] |
- Item 7505402 7-Dec-89 06:21
-
- From: D1950 CSG, Don Phillips,PRT
-
- To: MACAPP.TECH$ MacApp Technical
-
- Sub: Failures & Initialization
-
- How do I prepare TTObject1.IObject1's failure handler for the situation where
- its reference to Object2 is invalid because TTObject2.IObject2's failure
- handler has already freed Object2?
-
- From MacApp Cookbook: 'Handling errors when creating and initializing objects':
-
- 'The failure handler should do any necessary cleanup, and then call Free.'
-
- 'When you call the initialization method of an object and it fails, your
- reference to the object may become invalid. If the code calling the
- initialization method has a failure handler, the handler must be prepared
- for this situation.'
-
- How do I prepare my failure handler for this situation?
-
- It seems to me that a Failure Handler should not free the object itself, but
- should leave this to the calling method's Failure Handler???
-
- Using the example shown below, if there is a failure in IObject2, its failure
- handler will be executed and the object itself will be freed. When IObject1's
- failure handler is called next, it will attempt to free an object (tmpObjRef)
- which has already been freed. Error!
-
- Thanks for any explanations,
- Jo-Anne Droogh
-
-
- {*** Type Declarations ***}
- TObject1 = Object( TObject3 )
- ffObjRef : TObject2;
- PROCEDURE TObject1.IObject1;
-
- TObject2 = Object( TObject3 )
- PROCEDURE TObject2.IObject2;
-
- { *** Implementation *** }
- PROCEDURE TObject1.IObject1;
- VAR
- tmpObjRef : TObject2;
- fi : FailInfo;
-
- PROCEDURE LocalFailureHandler( error : OSErr; message : LONGINT );
- { According to the cookbook, this should cleanup and call Free }
-
- BEGIN
- { ... other cleanup ... }
- FreeIfObject( tmpObjRef );
- SELF.Free;
- END;
-
- BEGIN
- { First, initialize variables that Free needs to operate successfully. }
- SELF.ffObjRef := NIL;
- tmpObjRef := NIL;
-
- { Next, call the immediate ancestor's initialization method, if any. }
- INHERITED IObject3;
-
- { Since I do some initialization that can fail, set up a failure handler,
- do the initialization, then remove the failure handler. }
- CatchFailures( fi, LocalFailureHandler );
-
- { ... other code that can fail ... }
-
- New( tmpObjRef );
- FailNIL( tmpObjRef );
- tmpObjRef.IObject2;
-
- Success( fi );
-
- SELF.ffObjRef := tmpObjRef;
- END;
-
- PROCEDURE TObject2.IObject2;
-
- VAR
- fi : FailInfo;
-
- PROCEDURE LocalFailureHandler( error : OSErr; message : LONGINT );
- { According to the cookbook, this should cleanup and call Free }
-
- BEGIN
- SELF.Free;
- END;
-
- BEGIN
- { ... other stuff ... }
- CatchFailures( fi, LocalFailureHandler );
- { ... stuff that can fail ... }
- Success( fi );
- END;
-
-
-